<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 模块管理
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Rule as RuleModel;
use  app\admin\model\Common as CommonModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lib\ArrayMethod;

class RuleController extends Controller{

	private $model;
    private $login;
	/**
     * 初始化自动连接方法
     * @param array $module 
     * @param array $where 
    */
	public function __construct()  
    {    
        $this->model = new RuleModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            // echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
    } 		

	/**
     * 模块管理
     *  * @param array $department
     * @param array $where
     */
    public function list()
    {
            $this->display('list.html');

    }
	/**
     * 模块列表
     * @param array $data 
     * @param array $where 
     */
	public function data(){
	    $datalist = $this->model->list('dels<1');
            $data = array(
                "code" => 200,
                "data" => array_values(ArrayMethod::recurse($datalist)),
                "count"=> 0,
                "msg" => 'true'
            );
            $this->echojson($data);
    }

	/**
     * 模块编辑
     * @param array $add 
     * @param array $where 
    */
	public function add(){
	$params = post();
       if($params){
           //post请求则保存
           $datainfo = $this->model->save($params);
           $this->echoJson($datainfo);
       }else{

           $iconlist=(new CommonModel())->iconlist();
           $subfunctionlist=(new CommonModel())->subfunctionlist();

           if(get('id')){
                $moduleinfo = $this->model->info(get('id'));
               if($moduleinfo){
                   $moduleinfo['operate']=json_decode(stripslashes($moduleinfo['operate']),true);
                }
           }
            //定义变量moduleinfo输出
            $moduleinfo['id']=isset($moduleinfo['id'])?$moduleinfo['id']:'';
            $moduleinfo['title']=isset($moduleinfo['title'])?$moduleinfo['title']:'';
            $moduleinfo['code']=isset($moduleinfo['code'])?$moduleinfo['code']:'';
            $moduleinfo['langkey']=isset($moduleinfo['langkey'])?$moduleinfo['langkey']:'';
            $moduleinfo['icon']=isset($moduleinfo['icon'])?$moduleinfo['icon']:'';
            $moduleinfo['pid']=isset($moduleinfo['pid'])?$moduleinfo['pid']:0;
            $moduleinfo['is_top']=isset($moduleinfo['is_top'])?$moduleinfo['is_top']:0;
            $moduleinfo['status']=isset($moduleinfo['status'])?$moduleinfo['status']:0;
            $moduleinfo['url']=isset($moduleinfo['url'])?$moduleinfo['url']:'';
            $moduleinfo['orderid']=isset($moduleinfo['orderid'])?$moduleinfo['orderid']:0;
            $moduleinfo['operate']=isset($moduleinfo['operate'])?$moduleinfo['operate']:'';
           
           $this->assign('iconlist', $iconlist);
           $this->assign('modulelist', ArrayMethod::recurse($subfunctionlist));
           $this->assign('datainfo', $moduleinfo);
           $this->display('add.html');
       }
    }

    /**
     * 模块删除
     * @param array $del 
     * @param array $where 
     */
    public function del()
   {
       $id = post('id',false);
       $datadel = $this->model->del($id);
       $this->echoJson($datadel);
   }

}